package morning.cat.mybatis.plus.sample;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import morning.cat.mybatis.plus.sample.entity.User;
import org.junit.Test;

import java.util.List;


public class ApplicationTests2 extends BaseTests {

    @Test
    public void contextLoads() {
        // 初始化 User 对象
        User user = new User();
        // 插入 User (插入成功会自动回写主键到实体类)
        user.setName("Tom");
        user.setAge(23);
        boolean isSuccess = user.insert();
        System.out.println(isSuccess);

    }


    @Test
    public void contextLoads2() {
        User user = new User();
        user.setId(18);
        user.setAge(18);
        boolean isSuccess = user.updateById();
        System.out.println(isSuccess);
    }


    @Test
    public void contextLoads3() {
        User user = new User();
        user.setId(11);
        User exampleUser = user.selectById(user.getId());
        System.out.println(exampleUser);
    }


    @Test
    public void contextLoads4() {
        User user = new User();
        // 查询姓名为‘Tom’的所有用户记录
        List<User> userList = user.selectList(
                new EntityWrapper<User>().eq("name", "Tom")
        );

        userList.forEach(System.out::println);


    }


    @Test
    public void contextLoads5() {
        User user = new User();
        user.setId(4);
        // 删除 User
        boolean isSuccess = user.deleteById(user.getId());
        System.out.println(isSuccess);
    }


    @Test
    public void contextLoads11() {
        User user = new User();
        // 分页查询  姓名为‘Tom’的用户记录
        Page<User> userList = user.selectPage(
                // 第 current 页，页大小 size
                new Page<User>(2, 3),
                new EntityWrapper<User>().eq("name", "Tom")
        );

        userList.getRecords().forEach(System.out::println);
    }

    @Test
    public void contextLoads12() {
        User user = new User();
        // 分页查询 10 条姓名为‘Tom’，且年龄在18至50之间的用户记录
        Page<User> userList = user.selectPage(
                new Page<User>(2, 3),
                new EntityWrapper<User>().eq("name", "Tom")
                        .between("age", "1", "10")
        );
        userList.getRecords().forEach(System.out::println);
    }


}

